package live.yanxiaohui.demo.work;

import com.rabbitmq.client.*;
import live.yanxiaohui.demo.util.RabitMQConnection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
 * @Description 工作模式，手动ack
 * @CSDN https://blog.csdn.net/yxh13521338301
 * @Author yanxh<br>
 * @Date 2020/6/28 16:18<br>
 * @Version 1.0<br>
 */
public class Consumer {
    private static final String QUEUE_NAME = "game";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.创建连接
        Connection connection = RabitMQConnection.getConnection();
        // 2.设置通道
        final Channel channel = connection.createChannel();
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body, "UTF-8");
                System.out.println("消费者获取消息:" + msg);
                // 手动应答
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };
        // 3.监听队列
        channel.basicConsume(QUEUE_NAME, false, defaultConsumer);


    }
}
